import { getCampusSelector } from '@/api/campus/campus'
import request from '@/utils/request'
export default {
  components: {},
  data() {
    return {
      loading: false,
      visible: false,
      isDetail: false,
      dataForm: {
        id: '',
        brand: undefined,
        campusId: undefined,
        enCode: undefined,
        manager: undefined,
        managerMobile: undefined,
        area: undefined,
        businessName: undefined,
        businessPic: [],
        address: undefined,
        campusStatus: undefined,
        violationIssue: undefined,
        description: undefined,
        attachments: [],
        status: '0'
      },
      brandOptions: [],
      campusOptions: [],
      CampusStatusOptions: [],
      violationlssueOptions: [
        {
          enCode: '1',
          fullName: '合同解除，未拆品牌标识'
        },
        {
          enCode: '2',
          fullName: '品牌竞业，违约经营'
        },
        {
          enCode: '3',
          fullName: '贝尔品牌，其他违约'
        },
        {
          enCode: '4',
          fullName: '其它机构侵权'
        }
      ],
      rules: {
        brand: [
          {
            required: true,
            message: '请选择品牌',
            trigger: 'change'
          }
        ],
        campusId: [
          {
            required: true,
            message: '请选择校区',
            trigger: 'change'
          }
        ],
        area: [
          {
            required: true,
            message: '请选择区域',
            trigger: 'change'
          }
        ],
        address: [
          {
            required: true,
            message: '请输入详细地址',
            trigger: 'blur'
          }
        ],
        violationIssue: [
          {
            required: true,
            message: '请选择违规事项',
            trigger: 'change'
          }
        ],
        description: [
          {
            required: true,
            message: '请输入违规说明',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {},
  watch: {
    'dataForm.brand': {
      async handler(newVal, oldVal) {
        if (newVal !== oldVal) {
          try {
            this.campusLoading = true
            await getCampusSelector({ brand: newVal, hasCase: false, fields: 'id,fullName' }).then(res => {
              this.campusOptions = Array.isArray(res.data) ? res.data : []
            })
          } catch (err) {
            console.error('加载校区下拉选项失败:', err)
            this.$message.error('校区选项加载失败')
            this.campusOptions = []
          } finally {
            this.campusLoading = false
          }
        }
      }
    },
    'dataForm.campusId': {
      handler(newVal, oldVal) {
        this.loading = true
        if (newVal !== oldVal) {
          if (this.dataForm.brand !== 'other') {
            this.dataForm = Object.assign(this.dataForm, {
              enCode: undefined,
              manager: undefined,
              managerMobile: undefined,
              area: undefined,
              CampusStatus: undefined,
              businessName: undefined,
              businessPic: [],
              address: undefined
            })
            request({
              url: `/api/campus/${newVal}/info`,
              method: 'get'
            }).then(res => {
              this.dataForm = Object.assign(this.dataForm, {
                enCode: res.data.enCode,
                manager: res.data.manager,
                managerMobile: res.data.managerMobile,
                area: res.data.area,
                CampusStatus: res.data.status,
                businessName: res.data.businessName,
                address: res.data.address
              })
              console.log(this.dataForm, 'this.dataForm')
              this.loading = false
            })
          }
        }
      }
    }
  },
  created() {
    this.brandOptions = []
    this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
      res.push({ enCode: 'other', fullName: '非贝尔安亲体系' })
      this.brandOptions = res
    })
    this.$store.dispatch('base/getDictionaryData', { sort: 'CampusStatus' }).then(res => {
      this.CampusStatusOptions = res
    })
  },
  mounted() {},
  methods: {
    goBack() {
      this.$emit('refresh')
    },
    init(row, isDetail, campusId) {
      this.dataForm = {
        id: row ? row.id : 0,
        brand: undefined,
        campusId: campusId,
        enCode: undefined,
        manager: undefined,
        managerMobile: undefined,
        area: undefined,
        businessName: undefined,
        businessPic: [],
        address: undefined,
        campusStatus: undefined,
        violationIssue: undefined,
        description: undefined,
        attachments: []
      }
      this.visible = true
      this.isDetail = isDetail || false
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields()
        if (this.dataForm.id) {
          request({
            url: '/api/campus/violationscases/' + this.dataForm.id,
            method: 'get'
          }).then(res => {
            this.dataForm = Object.assign(this.dataForm, {
              description: res.data.description,
              attachments: res.data.attachments,
              brand: row.brand,
              campusId: res.data.campusId,
              area: res.data.area,
              businessName: res.data.businessName,
              businessPic: res.data.businessPic,
              address: res.data.address,
              violationIssue: res.data.violationIssue
            })
            console.log(this.dataForm, 'this.dataForm')
          })
        }
      })
    },
    dataFormSubmit() {
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          console.log(this.dataForm)
          if (!this.dataForm.id) {
            request({
              url: `/api/campus/violationscases`,
              method: 'post',
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.visible = false
                  this.$emit('refresh', true)
                }
              })
            })
          } else {
            request({
              url: '/api/campus/violationscases/' + this.dataForm.id,
              method: 'PUT',
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.visible = false
                  this.$emit('refresh', true)
                }
              })
            })
          }
        }
      })
    }
  }
}
